<?php
	/**
	 * Pagination configuration class
	 *
	 * @package Pagination
	 * @author Perkin Anton, Rostislav Rotaru
	 * @version 3.0
	 *
	 */
	class PaginationConfig
	{
		
		/* FIELDS */

		/**
		 * The default pagination theme
		 *
		 * @var string
		*/
		static $defaultPaginationTheme = 'default';
		
		/**
		 * The default pagination renderer class
		 *
		 * @var string
		*/
		static $defaultPaginationRenderClass = 'standardpagination';

		/**
		 * The default pagination item renderer class
		 *
		 * @var string
		*/
		static $defaultPaginationItemRenderClass = 'standardpaginationitem';

		/**
		 * The selected pagination item renderer class
		 *
		 * @var string
		*/
		static $defaultSelectedPaginationItemRenderClass = 'selectedpaginationitem';
		
		/**
		 * The default pagination CSS class
		 *
		 * @var string
		*/
		static $defaultPaginationCssClass = 'pagination';
		
		/**
		 * The default pagination link CSS class
		 *
		 * @var string
		*/
		static $defaultPaginationItemCssClass = 'pagination_item';
		
		/**
		 * The default selected pagination item CSS class
		 *
		 * @var string
		*/
		static $defaultSelectedPaginationItemCssClass = 'pagination_item';
		
		/**
		 * default input CSS class
		 * @var string
		 */
		static $defaultCssInputClass = 'custom_page';

		/**
		 * default gap CSS class
		 * @var string
		 */
		static $defaultCssGapClass = 'gap';
		
		/**
		 * default selected page
		 * @var string
		 */
		static $defaultSelectedPage = 1;

		/**
		 * default start page
		 * @var string
		 */
		static $defaultStartPage = 1;

		/**
		 * default step
		 * @var string
		 */
		static $defaultStep = 1;

		/**
		 * default use custom page number
		 * @var string
		 */
		static $defaultUseCustomPageNumber = false;
		
		/**
		 * default custom page number text
		 * @var string
		 */
		static $defaultCustomPageNumberTitle = '#';
		
		
		/* METHODS */
		
		/**
		* Parses schema and sets the defaults if necessary
		*
		* @param array $schema pagination configuration schema to analize
		* @return array parsed schema
		*/
		static function ParseSchemaDefaults($schema)
		{
			//Check if schema is not null
			if (!isset($schema)) return null;
			
			//Get pagination name if empty
			$schema['name'] = (!isset($schema['name'])) ? uniqid() : $schema['name'];
			
			//Check pagination theme
			$schema['theme'] = (!isset($schema['theme'])) ? strtolower(self::$defaultPaginationTheme) : $schema['theme'];
			
			//Check pagination render class
			$schema['render_class'] = (!isset($schema['render_class'])) ? strtolower(self::$defaultPaginationRenderClass) : $schema['render_class'];
			
			//css class
			$schema['css_class'] = (!isset($schema['css_class'])) ? $schema['theme'].'_'.self::$defaultPaginationCssClass : $schema['theme'].'_'.$schema['css_class'];					

			//input css class
			$schema['css_input_class'] = (!isset($schema['css_input_class'])) ? $schema['theme'].'_'.self::$defaultCssInputClass : $schema['theme'].'_'.$schema['css_input_class'];					
			
			//gap css class
			$schema['css_gap_class'] = (!isset($schema['css_gap_class'])) ? $schema['theme'].'_'.self::$defaultCssGapClass : $schema['theme'].'_'.$schema['css_gap_class'];					

			//pages number
			$schema['pages_number'] = ((!isset($schema['elements_number'])) || (!isset($schema['elements_per_page']))) ? $schema['pages_number'] : self::GetPagesNumber($schema['elements_number'], $schema['elements_per_page']);
			
			//start page
			$schema['start_page'] = (!isset($schema['start_page'])) ? self::$defaultStartPage : $schema['start_page'];
			
			//start page
			$schema['step'] = (!isset($schema['step'])) ? self::$defaultStep : $schema['step'];					

			//selected page
			$schema['selected_page'] = (!isset($schema['selected_page'])) ? self::$defaultSelectedPage : (($schema['selected_page'] < $schema['start_page']) ? $schema['start_page'] : (($schema['selected_page'] > $schema['pages_number']) ? ($schema['pages_number']) : $schema['selected_page']));
			
			//pages visible around the selected page
			$schema['pages_visible_around'] = (!isset($schema['pages_visible_around'])) ? $schema['pages_number'] : $schema['pages_visible_around'];					
			
			//pages visible on the sides
			$schema['pages_visible_sides'] = (!isset($schema['pages_visible_sides'])) ? $schema['pages_number'] : $schema['pages_visible_sides'];					

			//defines whether to use custom page number
			$schema['use_custom_page_number'] = (!isset($schema['use_custom_page_number'])) ? self::$defaultUseCustomPageNumber : $schema['use_custom_page_number'];					

			//custom page number title
			$schema['custom_page_number_title'] = (!isset($schema['custom_page_number_title'])) ? self::$defaultCustomPageNumberTitle : $schema['custom_page_number_title'];					
			
			//parses schema items defaults
			for ($i = $schema['start_page']; $i <= $schema['pages_number']; $i = $i + $schema['step'])
			{
				$schema['items'][$i] = self::ParseSchemaItemDefaults($i, $schema);
			}
			
			//visible page numbers
			$schema['visible_items'] = self::GetVisiblePageNumbersList($schema);
			
			return $schema;
		}
		
		/**
		* Parses schema item and sets the defaults if necessary
		*
		* @param array $i page number
		* @return array parsed schema item
		*/
		static function ParseSchemaItemDefaults($i, $schema)
		{
			//Check item render class
			$item['render_class'] = (!isset($schema['item_render_class'])) ? self::$defaultPaginationItemRenderClass : $schema['item_render_class'];					
			
			//Check item render class
			$item['selected_render_class'] = (!isset($schema['selected_item_render_class'])) ? self::$defaultSelectedPaginationItemRenderClass : $schema['selected_item_render_class'];					
			
			//css item class
			$item['css_class'] = (!isset($schema['css_item_class'])) ? $schema['theme'].'_'.self::$defaultPaginationItemCssClass : $schema['theme'].'_'.$schema['css_item_class'];					

			//css item class
			$item['css_class_selected'] = (!isset($schema['css_item_class_selected'])) ? $schema['theme'].'_'.self::$defaultSelectedPaginationItemCssClass : $schema['theme'].'_'.$schema['css_item_class_selected'];					
			
			//item link attributes
			$item['page_link_attributes'] = (!isset($schema['page_link_attributes'])) ? '' : str_replace('{page}', $i, $schema['page_link_attributes']);					
			
			return $item;
		}
			
		/**
		* Check if given schema is in the pool
		*
		* @param string $class_name the name of the class to check
		* @param string $objectToCheck object to check
		* 
		* @return object found or created object
		*/
		static function CheckPool($className, $objectToCheck)
		{
			//Check if the instance of object is already in the pool
			if (PaginationPool::${$objectToCheck}[$className] != null)
			{
				$objectRenderer = PaginationPool::${$objectToCheck}[$className];
			}
			else //Create a new one and add to the pool
			{
				require_once(dirname(__FILE__).SEPARATOR.'render'.SEPARATOR.$objectToCheck.SEPARATOR.$className.'.php');
				$objectRenderer = new $className;
				
				PaginationPool::${$objectToCheck}[$className] = $objectRenderer;
			}
			return $objectRenderer;
		}
		
		/**
		 * Counts pages number
		 * @param $total total elements
		 * @param $perPage elements per page
		 * @return integer
		 */
		static function GetPagesNumber($total, $perPage)
		{
			return ceil($total/$perPage);
		}
		
		/**
		 * 
		 * 
		 * @param $schema schema
		 * @return array
		 */
		static function GetVisiblePageNumbersList($schema)
		{
			$result = array();
			
			for ($i = $schema['start_page']; $i <= $schema['pages_number']; $i = $i + $schema['step'])
			{
				if ( (($i - $schema['start_page']) < $schema['pages_visible_sides']) || (($schema['pages_number'] - $i) < $schema['pages_visible_sides'])
					|| (abs($i - $schema['selected_page']) <= $schema['pages_visible_around']) )
				{
					$result[] = $i;
				}
			}	

			return $result;
		}
	}
?>